package blackstar.plugin.ui;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import blackstar.plugin.ui.vo.JMXProxyInfo;
import blackstar.plugin.ui.vo.SystemInformation;

/**
 * @author RayWoo (ayufox@gmail.com)
 *
 */
public class AgentServlet extends HttpServlet
{
	private final static Log LOGGER = LogFactory.getLog(AgentServlet.class);
	
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		String uri = request.getRequestURI();
		if (!"/".equals(request.getContextPath()))
		{
			uri = uri.substring(request.getContextPath().length());
		}
		LOGGER.info("request:" + uri);
		String[] pathArray = uri.substring(1).split("/");
		if (pathArray.length == 1)
		{
			printJVM(request, response);
		}
		else if (pathArray.length == 2)
		{
			if ("overview".equals(pathArray[1]))
			{
				printSystemOverview(request, response);
			}
			else
			{
				response.sendError(404);
			}
		}
		else
		{
			response.sendError(404);
		}
	}

	

	private void printSystemOverview(HttpServletRequest request,
			HttpServletResponse response) throws IOException
	{
		response.setContentType("application/json;charset=utf-8");
		SystemInformation systemInforation = JMXProxyUtils
				.getSystemInformation();
		PrintWriter writer = response.getWriter();

		writer.print("{");
		writer.print("osName :'");
		writer.print(systemInforation.getOsName());
		writer.print("', osArch :'");
		writer.print(systemInforation.getOsArch());
		writer.print("', osVersion :'");
		writer.print(systemInforation.getOsVersion());
		writer.print("', osProcessors :'");
		writer.print(systemInforation.getOsProcessors());
		writer.print("', osLoadAvarage :'");
		writer.print(systemInforation.getOsLoadAvarage());
		writer.print("', osTotalMemorySize :'");
		writer.print(systemInforation.getOsTotalMemorySize());
		writer.print("', osFreeMemorySize :'");
		writer.print(systemInforation.getOsFreeMemorySize());
		writer.print("', osVirtualMemorySize :'");
		writer.print(systemInforation.getOsVirtualMemorySize());
		writer.print("', osTotalSwapMemorySize :'");
		writer.print(systemInforation.getOsTotalSwapMemorySize());
		writer.print("', osFreeSwapMemorySize :'");
		writer.print(systemInforation.getOsFreeSwapMemorySize());
		writer.print("'}");

		writer.flush();
	}

	private void printJVM(HttpServletRequest request,
			HttpServletResponse response) throws IOException
	{
		List<JMXProxyInfo> list = JMXProxyUtils.listProxys();
		PrintWriter writer = response.getWriter();
		writer.print("[");
		int index = 0;
		for (JMXProxyInfo info : list)
		{
			if (index > 0)
			{
				writer.print(",");
			}
			writer.print("{");
			writer.print("id : '");
			writer.print(info.getPid());
			writer.print("', name :'");
			writer.print(info.getId());
			writer.print("', monitor :");
			writer.print(info.isMonitorEnable());
			writer.print(", performance :");
			writer.print(info.isPerformanceEnable());
			writer.print("}");
			index++;
		}
		writer.print("]");
		writer.flush();
	}
}
